package top.hhydra.mimall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.hhydra.mimall.constant.MallConst;
import top.hhydra.mimall.entity.Category;
import top.hhydra.mimall.mapper.CategoryMapper;
import top.hhydra.mimall.service.ICategoryService;
import top.hhydra.mimall.vo.CategoryVo;
import top.hhydra.mimall.vo.ResponseVo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author SaiyaEmon
 * @since 2020-02-11
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public ResponseVo<List<CategoryVo>> categories() {
        List<CategoryVo> categoryVos = new ArrayList<>();

        List<Category> categories = baseMapper.selectList(null);
        for (Category category : categories) {
            if (category.getParentId().equals(MallConst.CATEGORY_ROOT_ID)) {
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, categoryVo);
                categoryVos.add(categoryVo);
                setSubCategories(categories, categoryVo);
            }
        }
        categoryVos.sort(Comparator.comparingInt(CategoryVo::getSortOrder));
        return ResponseVo.success(categoryVos);
    }

    @Override
    public void findSubCategoryId(Integer id, List<Integer> categoryIds) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "parent_id");
        List<Category> categories = baseMapper.selectList(queryWrapper);
        if (id.equals(MallConst.CATEGORY_ROOT_ID)) {
            categoryIds.addAll(categories.stream().map(Category::getId).collect(Collectors.toList()));
        }else{
            findSubCategoryId(id, categoryIds, categories);
        }
        log.info("总共查出 {} 个id", categoryIds.size());
        categoryIds.stream().forEach(e -> log.info(e.toString()));
    }

    private void findSubCategoryId(Integer id, List<Integer> categoryIds, List<Category> categories) {
        for (Category category : categories) {
            if (id.equals(category.getParentId())) {
                categoryIds.add(category.getId());
                findSubCategoryId(category.getId(), categoryIds, categories);
            }
        }
    }

    /**
     * 设置子类目
     *
     * @param categories
     * @param categoryVo
     */
    private void setSubCategories(List<Category> categories, CategoryVo categoryVo) {
        List<CategoryVo> subCategories = new ArrayList<>();
        for (Category category : categories) {
            if (category.getParentId().equals(categoryVo.getId())) {
                CategoryVo subCategoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, subCategoryVo);
                subCategories.add(subCategoryVo);
                categoryVo.setSubCategories(subCategories);
                setSubCategories(categories, subCategoryVo);
            }
        }
        subCategories.sort(Comparator.comparingInt(CategoryVo::getSortOrder));
    }
}
